/**
 * 
 */
package me.myachievements.web.administration.account;

import java.util.List;

import me.myachievements.persistence.account.Account;
import me.myachievements.persistence.account.AccountPersistenceManager;
import me.myachievements.web.PersistentObjectModel;
import me.myachievements.web.restricted.account.AccountDetailsPage;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * @author besseym
 *
 */
public class AccountListView extends PropertyListView<Account> {
	
	@SpringBean(name="accountPersistenceManager")
	AccountPersistenceManager accountPersistenceManager;

	/**
	 * Constructor
	 * @param id
	 */
	public AccountListView(String id) {
		super(id, new AccountListModel());
	}

	/*
	 * (non-Javadoc)
	 * @see org.apache.wicket.markup.html.list.ListView#populateItem(org.apache.wicket.markup.html.list.ListItem)
	 */
	@Override
	protected void populateItem(final ListItem<Account> item) {
		
		item.add(new Label("id"));
		item.add(new Label("username"));
		item.add(new Label("email"));
		item.add(new Label("name"));
		item.add(new Label("createdOn"));
		item.add(new Label("updatedOn"));
		
		final Account account = (Account)item.getModelObject();
		Long id = account.getId();
		
		PageParameters pageParameters = new PageParameters(); 
		pageParameters.add("id", id.toString());
		item.add(new BookmarkablePageLink<AccountDetailsPage>("view", AccountDetailsPage.class, pageParameters));
		item.add(new BookmarkablePageLink<UpdateAccountPage>("update", UpdateAccountPage.class, pageParameters));
		
		item.add(new Link("delete") {

			@Override
			public void onClick() {
				Account account = (Account)item.getModelObject();
				accountPersistenceManager.delete(account);
			}
		});
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.apache.wicket.markup.html.list.PropertyListView#getListItemModel(org.apache.wicket.model.IModel, int)
	 */
	@Override
	protected IModel<Account> getListItemModel(IModel listViewModel, int index) {
		
		AccountListModel accountListModel = (AccountListModel) listViewModel;
		Account account = accountListModel.getObject().get(index);
		return new CompoundPropertyModel<Account>(new PersistentObjectModel<Account>(account));
	}
}
